/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package libreriaTicket.dao;

import conexion.BD;
import conexion.SqlFormat;
import java.util.ArrayList;
import libreriaTicket.entity.Comuna;
import libreriaTicket.entity.Pais;
import libreriaTicket.entity.Region;

/**
 *
 * @author Duoc
 */
public class DaoRegion {

    public static boolean sqlInsert(Region region) {
        try {
            SqlFormat sql = new SqlFormat(SqlFormat.INSERTAR, "region");
            //sql.addField("codigo", region.getIdRegion());
            sql.addField("descripcion", region.getDescripcion());
            sql.addField("codPais", "1");//---Siempre sera 1, ya que siempre sera chile
            

            return BD.getInstance().sqlEjecutar(sql.crearInstruccion());



        } catch (Exception e) {
        }
        return false;
    }

    public static boolean sqlUpdate(Region region) {
        try {

            SqlFormat sql = new SqlFormat(SqlFormat.ACTUALIZAR, "region");
            sql.where("id", region.getIdRegion());
            sql.addField("descripcion", region.getDescripcion());
            sql.addField("codPais", region.getPais().getCodigo());


            return BD.getInstance().sqlEjecutar(sql.crearInstruccion());


        } catch (Exception e) {
        }
        return false;
    }

    public static boolean sqlDelete(Region region) {
        try {
            SqlFormat sql = new SqlFormat(SqlFormat.ELIMINAR, "region");
            sql.where("id", region.getIdRegion());

            return BD.getInstance().sqlEjecutar(sql.crearInstruccion());


        } catch (Exception e) {
        }
        return false;
    }

    public static Region sqlSelect(Region region) {

        try {
            Region region2 = new Region();
            SqlFormat sql = new SqlFormat(SqlFormat.SELECCIONAR, "region");
            sql.where("region", region.getIdRegion());

            BD.getInstance().sqlQuery(sql.crearInstruccion());
            if (!BD.getInstance().sqlFetch()) {
                return null;
            }

            region2.setDescripcion(BD.getInstance().getString("descripcion"));
            region2.setIdRegion(BD.getInstance().getInt("id"));
            region2.setPais(new Pais(BD.getInstance().getString("codPais")));


            return region2;
        } catch (Exception e) {
        }
        return null;
    }

    public static ArrayList<Region> sqlSelectAll() {
        ArrayList<Region> regions = new ArrayList<Region>();


        try {
            BD.getInstance().sqlQuery(SqlFormat.SelectAll("region"));
            while (BD.getInstance().sqlFetch()) {
                Region region = new Region();
                region.setDescripcion(BD.getInstance().getString("descripcion"));
                region.setIdRegion(BD.getInstance().getInt("codigo"));
                region.setPais(new Pais(BD.getInstance().getString("codPais")));
                
                regions.add(region);

            }
            return regions;
        } catch (Exception e) {
        }
        return null;
    }
}
